﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;

namespace StackExchangeRedisDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.0.201:6379");
            // 获取db
            IDatabase db = redis.GetDatabase(0);

            #region String 键值对
            // 设置值,默认永不过期
            db.StringSet("StackExchangeRedis:name", "Harry");
            // 获取
            Console.WriteLine(db.StringGet("StackExchangeRedis:name"));
            // 批量设置
            KeyValuePair<RedisKey, RedisValue>[] values = {
                new KeyValuePair<RedisKey, RedisValue>("StackExchangeRedis:key1", "value1"),
                new KeyValuePair<RedisKey, RedisValue>("StackExchangeRedis:key2", "value2"),
                new KeyValuePair<RedisKey, RedisValue>("StackExchangeRedis:key3", "value3"),
            };
            db.StringSet(values);
            // 批量获取
            RedisKey[] keys = { "StackExchangeRedis:key1", "StackExchangeRedis:key2" };
            var list = db.StringGet(keys);
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
            // 设置增量
            db.StringSet("StackExchangeRedis:count", 1);
            Console.WriteLine(db.StringGet("StackExchangeRedis:count"));
            db.StringIncrement("StackExchangeRedis:count");
            Console.WriteLine(db.StringGet("StackExchangeRedis:count"));

            #endregion

            #region Hash 哈希
            // 设置
            db.HashSet("StackExchangeRedis:person", "name", "Ken");
            HashEntry[] hashEntrys = {
                new HashEntry("sex", "男"),
                new HashEntry("age", 28),
            };
            db.HashSet("StackExchangeRedis:person", hashEntrys);
            // 获取
            Console.WriteLine($"person这个哈希中的age为{ db.HashGet("StackExchangeRedis:person", "age") }");
            var persions = db.HashValues("StackExchangeRedis:person");
            foreach (var item in persions)
            {
                Console.WriteLine($"{ item }");
            }
            #endregion

            #region list 列表(场景：消息队列)
            db.ListRightPush("StackExchangeRedis:list", "one");
            db.ListRightPush("StackExchangeRedis:list", "two");
            // 获取列表长度
            Console.WriteLine($"list的长度为{ db.ListLength("StackExchangeRedis:list") }");
            // 获取列表元素
            var listVaules = db.ListRange("StackExchangeRedis:list");
            foreach (var item in listVaules)
            {
                Console.WriteLine($"{ item }");
            }
            // 获取第一个并移除
            var listFirstValue = db.ListLeftPop("StackExchangeRedis:list");
            Console.WriteLine($"{ listFirstValue }");
            #endregion

            #region Set 集合
            db.SetAdd("StackExchangeRedis:students", "harry");
            db.SetAdd("StackExchangeRedis:students", new RedisValue[] { "ken", "lily" });
            Console.WriteLine($"students这个集合的大小为{ db.SetLength("StackExchangeRedis:students") }");

            db.SetAdd("StackExchangeRedis:teachers", new RedisValue[] { "ken", "test" });

            // 差集
            RedisValue[] diff = db.SetCombine(SetOperation.Difference, "StackExchangeRedis:students", "StackExchangeRedis:teachers");
            foreach (var item in diff)
            {
                Console.WriteLine($"students和teachers的差集：{ item }");
            }
            // 交集
            RedisValue[] inter = db.SetCombine(SetOperation.Intersect, "StackExchangeRedis:students", "StackExchangeRedis:teachers");
            foreach (var item in inter)
            {
                Console.WriteLine($"students和teachers的交集：{ item }");
            }
            // 并集
            RedisValue[] union = db.SetCombine(SetOperation.Union, "StackExchangeRedis:students", "StackExchangeRedis:teachers");
            foreach (var item in union)
            {
                Console.WriteLine($"students和teachers的并集：{ item }");
            }

            #endregion



            Console.ReadKey();
        }
    }
}
